package com.wang.exam.controller;

import com.wang.exam.mapper.AdminMapper;
import com.wang.exam.pojo.Admin;
import com.wang.exam.service.updatePasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UpdatePassWordController {
    @RequestMapping("/admin/update_password")
    public String updateAdminPassWord(){
        return "admin_up_password";
    }

    @Autowired
    updatePasswordService updateAdminPassword;
    @RequestMapping("/admin/updatePassword")
    public String adminUpPassWord(@RequestParam("old_admin_password") String pwd1,
                             @RequestParam("new_admin_password") String pwd2,
                             @RequestParam("re_new_admin_password") String pwd3,
                             Model model, HttpSession httpSession){
        String pwd = httpSession.getAttribute("admin_pwd").toString();
        String name = httpSession.getAttribute("loginUser").toString();
        if(pwd1.equals(pwd)){
            if(pwd1.equals(pwd2)){
                model.addAttribute("upAdmin","新密码与旧密码一致！");
                return "admin_up_password";
            }else {
                if (pwd2.equals(pwd3)) {
                    int n = updateAdminPassword.updateAdminPassword(pwd2, name);
                    if (n == 1) {
                        return "index";
                    } else {
                        model.addAttribute("upAdmin", "代码错误！修改失败！");
                        return "admin_up_password";
                    }
                } else {
                    model.addAttribute("upAdmin", "新密码两次输入不一致!");
                    return "admin_up_password";
                }
            }
        }else {
            model.addAttribute("upAdmin","旧密码输入错误！");
            return "admin_up_password";
        }
    }

    @RequestMapping("/teacher/updatePassword")
    public String teacherUpPassWord(@RequestParam("old_teacher_password") String pwd1,
                                  @RequestParam("new_teacher_password") String pwd2,
                                  @RequestParam("re_new_teacher_password") String pwd3,
                                  Model model, HttpSession httpSession){
        String pwd = httpSession.getAttribute("teacher_pwd").toString();
        String id = httpSession.getAttribute("loginTeacherId").toString();
        if(pwd1.equals(pwd)){
            if(pwd1.equals(pwd2)){
                model.addAttribute("upTeacher","新密码与旧密码一致！");
                return "teacher_up_password";
            }else {
                if (pwd2.equals(pwd3)) {
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("pwd",pwd3);
                    map.put("id",id);
                    int a = updateAdminPassword.upTeacherPassWord(map);
                    if (a == 1) {
                        return "index";
                    } else {
                        model.addAttribute("upTeacher", "代码错误！修改失败！");
                        return "teacher_up_password";
                    }
                } else {
                    model.addAttribute("upTeacher", "新密码两次输入不一致!");
                    return "teacher_up_password";
                }
            }
        }else {
            model.addAttribute("upTeacher","旧密码输入错误！");
            return "teacher_up_password";
        }
    }
    @RequestMapping("/student/updatePassword")
    public String studentUpPassWord(@RequestParam("old_student_password") String pwd1,
                                    @RequestParam("new_student_password") String pwd2,
                                    @RequestParam("re_new_student_password") String pwd3,
                                    Model model, HttpSession httpSession){
        String pwd = httpSession.getAttribute("student_pwd").toString();
        String id = httpSession.getAttribute("loginStudentId").toString();
        if(pwd1.equals(pwd)){
            if(pwd1.equals(pwd2)){
                model.addAttribute("upStudent","新密码与旧密码一致！");
                return "student_up_password";
            }else {
                if (pwd2.equals(pwd3)) {
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("pwd",pwd3);
                    map.put("id",id);
                    int a = updateAdminPassword.upStudentPassWord(map);
                    if (a == 1) {
                        return "index";
                    } else {
                        model.addAttribute("upStudent", "代码错误！修改失败！");
                        return "student_up_password";
                    }
                } else {
                    model.addAttribute("upStudent", "新密码两次输入不一致!");
                    return "student_up_password";
                }
            }
        }else {
            model.addAttribute("upStudent","旧密码输入错误！");
            return "student_up_password";
        }
    }
}
